package controller;

import implementor.BalanceImpl;
import implementor.SellerInfoImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import daoInterface.BalanceDao;
import daoInterface.SellerInfoDao;
import dto.SellerInfo;

import model.Balance;
import utilities.AppUtilites;
import utilities.ViewUtilites;
import view.balance.BalanceView;

public class BalanceController implements ActionListener{
	private BalanceView view;
	private Balance model;
	
	public BalanceController() {
		
	}

	public BalanceController(BalanceView view, Balance model) {
		super();
		this.view = view;
		this.model = model;
		view.actionlisteners(this);
		AppUtilites.setSellerNameCombo(view.getComboBox());
	}

	public BalanceView getView() {
		return view;
	}

	public void setView(BalanceView view) {
		this.view = view;
	}

	public Balance getModel() {
		return model;
	}

	public void setModel(Balance model) {
		this.model = model;
	}
	
	
	public void reactOnSave() throws Exception
	{
		String sellerName=view.getComboBox().getSelectedItem().toString();
		double totalAmt=Double.parseDouble(view.getTextField().getText());
		double amtPaid=Double.parseDouble(view.getTextField_1().getText());
		Date date=ViewUtilites.convert(view.getLblClick().getText());
		
		//model=new Balance(sellerName, totalAmt, amtPaid, date);
		
		BalanceDao bd=new BalanceImpl();
		bd.add(model);
	}
	
	public void reactOnUpdate() throws Exception
	{
		/*model=new Balance();
		model.setSellerName(view.getComboBox().getSelectedItem().toString());
		BalanceDao bdao=new BalanceImpl();
		Balance b=bdao.retrieve("sellerName", model.getsellerId());
		double previousAmt=b.getAmtPaid();
		double recentAmtPaid=previousAmt+Double.parseDouble(view.getTextField_1().getText());
		model.setAmtPaid(recentAmtPaid);
		model=new Balance(1, model.getSellerName(), b.getTotalAmt(), model.getAmtPaid(),b.getPurchaseDate());
		bdao.update(model);*/
		
	}

	@Override
	public void actionPerformed(ActionEvent a) {
		String k=a.getActionCommand();
		
		if(k.contains("SAVE"))
		{
			try {
				reactOnSave();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(k.contains("UPDATE"))
		{
			try {
				reactOnUpdate();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(k.contains("CLICK"))
		{
			ViewUtilites.makeDateComponent(view.getLblClick().getText());
		}
		
	}
	
	
	
	
}
